# -*- coding: utf-8 -*-
from dialog import *
from tasks import *
from sdk import *
import json
import os


def get_config():
    global domain, username, password
    global ak, sk, obsServer, bucketName, repoPath
    if os.path.exists('./config.json'):
        with open('./config.json') as json_file:
            data = json.load(json_file)
            if data.get('domain'):
                domain = data['domain']
            else:
                domain = ''
            if data.get('username'):
                username = data['username']
            if data.get('password'):
                password = data['password']
            if data.get('ak'):
                ak = data['ak']
            else:
                ak = ''
            if data.get('sk'):
                sk = data['sk']
            else:
                sk = ''
            if data.get('obsServer'):
                obsServer = data['obsServer']
            else:
                obsServer = ''
            if data.get('bucketName'):
                bucketName = data['bucketName']
            else:
                bucketName = ''
            if data.get('repoPath'):
                repoPath = data['repoPath']
            else:
                repoPath = ''
        return True
    else:
        return False

if __name__ == '__main__':
    global domain, username, password
    global ak, sk, obsServer, bucketName, repoPath
    lang = select_lang()
    if get_config():
        print("Config file loaded.")
    else:
        domain = get_domain(lang)
        username = get_username(lang)
        password = get_password(lang)
    
    region = get_region(lang)
    client = DevCloud(region, username, password, domain)
    task = get_task(lang)
    while(task != "quit" ):
        if task == 'backup':
            backup(client)
        elif task == 'restore':
            restore(client)
        elif task == 'move':
            repos_file = get_file_path(lang)
            move(client, repos_file)
        elif task == 'display':
            display(client)
        elif task == 'download':
            repos_file = get_file_path(lang)
            download(repos_file, repoPath)
        elif task == "uploadRepos":
            uploadRepos(ak, sk, obsServer, bucketName, repoPath)
        task = get_task(lang)